package com.hmusic.mymusic.controller;

import com.alibaba.fastjson.JSONObject;
import com.hmusic.mymusic.domain.Comment;
import com.hmusic.mymusic.domain.Singer;
import com.hmusic.mymusic.service.CommentService;
import com.hmusic.mymusic.utils.Consts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 评论controller层
 */
@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 添加评论
     */
    @PostMapping("/add")
    public Object addComment(@RequestBody Comment comment){
        JSONObject jsonObject = new JSONObject();
        try{
            commentService.insert(comment);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"评论成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"评论失败");
            return jsonObject;
        }
    }

    /**
     * 修改评论
     */
    @PostMapping("/update")
    public Object updateComment(@RequestBody Comment comment){
        JSONObject jsonObject = new JSONObject();
        try{
            commentService.update(comment);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"修改成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"修改失败");
            return jsonObject;
        }
    }

    /**
     * 删除评论
     */
    @GetMapping("/delete")
    @ResponseBody
    public Object deleteComment(String id){
        JSONObject jsonObject = new JSONObject();
        try{
            commentService.delete(id);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"删除成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"删除失败");
            return jsonObject;
        }
    }

    /**
     * 根据主键查询整个对象
     */
    @GetMapping("/selectByPrimaryKey")
    @ResponseBody
    public Object selectByPrimaryKey(String id){
        return commentService.selectByPrimaryKey(id);
    }

    /**
     * 歌曲评论
     */
    @GetMapping("/commentOfSongId")
    @ResponseBody
    public Object commentOfSongId(String song_id){
        return commentService.commentOfSongId(song_id);
    }

    /**
     * 歌单评论
     */
    @GetMapping("/commentOfSongListId")
    @ResponseBody
    public Object commentOfSongListId(String song_list_id){
        return commentService.commentOfSongListId(song_list_id);
    }

    /**
     * 计算歌曲评论数
     */
    @GetMapping("/CountSongComment")
    @ResponseBody
    public Object CountSongComment(String song_id){
        return commentService.CountSongComment(song_id);
    }

    /**
     * 计算歌曲评论数
     */
    @GetMapping("/CountSongListComment")
    @ResponseBody
    public Object CountSongListComment(String song_list_id){
        return commentService.CountSongListComment(song_list_id);
    }

}
